//
//  PictureDetailViewController.m
//  QT
//
//  Created by 甘世清 on 2024/10/24.
//

#import "PictureDetailViewController.h"
#import "PictureDetailPopView.h"
#import "AddPictureViewController.h"
@interface PictureDetailViewController ()<TZImagePickerControllerDelegate>
@property (nonatomic , strong) HeaderView * headerView;
@property (nonatomic , strong) UILabel * nameLabel;
@property (nonatomic , strong) UILabel * timeLabel;
@property (nonatomic , strong) UIImageView * pictureImageView;
@property (nonatomic ,strong) UIVisualEffectView * visualEffectView;
@property (nonatomic , strong) SQCustomButton * btn;
@end

@implementation PictureDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"名字名字");
}
- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.timeLabel];
    [self.view addSubview:self.pictureImageView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(48);
            make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView);
        make.left.equalTo(self.headerView.mas_right).offset(10);
        make.height.mas_equalTo(20);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.headerView.mas_bottom);
        make.left.equalTo(self.headerView.mas_right).offset(10);
        make.height.mas_equalTo(20);
    }];
    [self.pictureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.top.equalTo(self.headerView.mas_bottom).offset(10);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-120);
    }];
    [self.view addSubview:self.visualEffectView];
    [self.visualEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.pictureImageView);
    }];
    
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.pictureImageView);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.pictureImageView.mas_bottom).offset(30);
    }];
    [self SQ_Header];
   
}
-(void)SQ_Header{
    [self.headerView.headerImage sd_setImageWithURL:SQURLImage(@"https://files-ft.oss-cn-chengdu.aliyuncs.com/file/6666cfe627349fc31fac16b2.jpg") placeholderImage:SQGetImage(@"1")];
    self.nameLabel.text = @"名字名字";
    self.timeLabel.text = @"2024-10-29";
    [self.pictureImageView sd_setImageWithURL:SQURLImage(@"https://files-ft.oss-cn-chengdu.aliyuncs.com/file/6666cfe627349fc31fac16b2.jpg") placeholderImage:SQGetImage(@"1")];
    [self.view layoutIfNeeded];
    WS(weakSelf);
    [self.btn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if (button.tag==1000) {
            [weakSelf SQ_ButtonClick];
        } else {
            UIImageWriteToSavedPhotosAlbum(weakSelf.pictureImageView.image, weakSelf, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }];
}
#pragma mark -- 保存图片结果
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
     if (!error) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"已下载,请在相册中查看")];
    } else {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"保存失败,请重试!")];
    }
}
-(void)SQ_ButtonClick{
    [self SQ_PopView];
    return;
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf.visualEffectView removeFromSuperview];
        weakSelf.btn.text = SQNSLocalString(@"保存到相册");
        weakSelf.btn.tag = 1001;
    }];
}
-(void)SQ_PopView{
    PictureDetailPopView * vc = [[PictureDetailPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc.xiangjiBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
        AddPictureViewController * vc = [[AddPictureViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
}
-(void)SQ_HeaderViewButtonClick{
    PeopleDetailsViewController * vc = [[PeopleDetailsViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
}

- (HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc]init];
        _headerView.layer.cornerRadius = 24;
        _headerView.layer.masksToBounds = YES;
        _headerView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        [_headerView.btn addTarget:self action:@selector(SQ_HeaderViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = SQColor333333;
        _nameLabel.font = FONT_MEDIUM(16);
    }
    return _nameLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.textColor = SQColor666666;
        _timeLabel.font = FONT_REGULAR(14);
    }
    return _timeLabel;
}
- (UIImageView *)pictureImageView{
    if (!_pictureImageView) {
        _pictureImageView = [[UIImageView alloc]init];
        _pictureImageView.contentMode = UIViewContentModeScaleAspectFill;
        _pictureImageView.clipsToBounds = YES;
        _pictureImageView.layer.cornerRadius = 10;
        _pictureImageView.layer.masksToBounds = YES;
    }
    return _pictureImageView;
}

- (UIVisualEffectView *)visualEffectView{
    if (!_visualEffectView) {
        UIBlurEffect * blureffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blureffect];
        _visualEffectView.layer.cornerRadius = 10;
        _visualEffectView.layer.masksToBounds = YES;
    }
    return _visualEffectView;
}
- (SQCustomButton *)btn{
    if (!_btn) {
        _btn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _btn.backgroundColor = SQColorDA70D6;
        _btn.layer.cornerRadius = 25;
        _btn.layer.masksToBounds = YES;
        _btn.text = SQNSLocalString(@"交换照片");
        _btn.font = FONT_MEDIUM(14);
        _btn.tag = 1000;
        _btn.textColor = UIColor.whiteColor;
    }
    return _btn;
}
@end
